import TencentCloudChat from '@tencentcloud/chat'
import TIMUploadPlugin from 'tim-upload-plugin'
import { genTestUserSig } from '../../TUIKit/debug/GenerateTestUserSig'
Page({
  data: {
    config: {}
  },
  onLoad(options) {
    const app = getApp()
    this.setData({
      config: {
        userID: String(
          app.globalData.userInfo.unionId
        ),
        SDKAPPID: 1600038086,
        SECRETKEY:
          '418fd665deb14ef2eafdbddfdcc7d8df30924cc40e3b6e1f1c2d0213199c4730',
        EXPIRETIME: 604800
      }
    })
    const userSig = genTestUserSig(
      this.data.config
    ).userSig
    wx.$TUIKit = TencentCloudChat.create({
      SDKAppID: this.data.config.SDKAPPID
    })
    wx.$chat_SDKAppID = this.data.config.SDKAPPID
    wx.$chat_userID = this.data.config.userID
    wx.$chat_userSig = userSig
    wx.TencentCloudChat = TencentCloudChat
    wx.$TUIKit.registerPlugin({
      'tim-upload-plugin': TIMUploadPlugin
    })
    wx.$TUIKit.login({
      userID: this.data.config.userID,
      userSig
    })
    wx.setStorage({
      key: 'currentUserID',
      data: []
    })
    wx.$TUIKit.on(
      wx.TencentCloudChat.EVENT.SDK_READY,
      this.onSDKReady,
      this
    )
  },
  onUnload() {
    wx.$TUIKit.logout()
    wx.$TUIKit.off(
      wx.TencentCloudChat.EVENT.SDK_READY,
      this.onSDKReady,
      this
    )
  },
  onSDKReady() {
    const TUIKit = this.selectComponent('#TUIKit')
    TUIKit.init()
  }
})
